package com.lamp.gallery;

import java.util.List;

import com.lamp.JsonClass.PhotoStatus;
import com.lamp.downloadimage.BitmapCache;
import com.lamp.downloadimage.ImageDownloader;
import com.lamp.downloadimage.MyImageDownloader;
import com.lamp.main.R;
import android.R.integer;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class MyFlipper extends FrameLayout implements
		GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
	private Animation inFromLeft;
	private Animation outToRight;
	private Animation inFromRight;
	private Animation outToLeft;
	private ViewFlipper flipper;
	private MyImageView img1;
	private MyImageView img2;
	private GestureDetector gestureDetector = null;
	private MyImageDownloader imageDownloader;
	private List<PhotoStatus> photos;
	private int indexPhotos = 0;
	private MyGalleyListener myListener;
	private Context context;
	
	public MyFlipper(Context context) {
		super(context);
		init(context);
		this.context = context;
		ProgressDialog.show(context, null, "s");
	}

	public MyFlipper(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
		this.context = context;
		// TODO Auto-generated constructor stub
	}
	
	public int getIndex(){
		return indexPhotos;
	}
	
	public void setDetails(BitmapCache bitmapGallery , List<PhotoStatus> photos , int index){
		imageDownloader = new MyImageDownloader(bitmapGallery);
		this.photos = photos;
		indexPhotos = index;
		replacePhotos();
		for (PhotoStatus p : photos){
			Log.d("ImageURL", p.getPhotoURL());
				
		}
		setImages();
	}
	
	
	public void replacePhotos(){
		for (PhotoStatus p : photos){
			String oldUrl;
			oldUrl = p.getPhotoURL();
			String newUrl;
			newUrl = replaceSize(oldUrl);
			p.setPhotoURL(newUrl);
		}
	}
	
	public String  replaceSize(String str){
		
		Display display =((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		int width = display.getWidth();
		int height = display.getHeight();
		
		str = str.replace("&w=60", "&w="+width);
		str = str.replace("&h=60", "&h="+height);
		Log.d("REPLACE STR", str);
		return str;
	}
	
	
	
	
	public void init(Context context){
		inFromLeft = AnimationUtils.loadAnimation(context, R.anim.in_from_left);
		outToRight = AnimationUtils.loadAnimation(context, R.anim.out_to_right);
		inFromRight = AnimationUtils.loadAnimation(context,
				R.anim.in_from_right);
		outToLeft = AnimationUtils.loadAnimation(context, R.anim.out_to_left);
		
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		
		flipper = new ViewFlipper(context);
		flipper.setLayoutParams(params);
		flipper.setPadding(0, 0, 0, 0);	
		addView(flipper);
		img1 = new MyImageView(context);
		img1.setTag("IMG1");
		img1.setLayoutParams(params);
		img1.setBackgroundColor(Color.BLACK);
		img1.setPadding(0, 0, 0, 0);
		flipper.addView(img1);
		img2 = new MyImageView(context);
		img2.setTag("IMG2");
		img2.setLayoutParams(params);
		flipper.addView(img2);
		flipper.setBackgroundColor(Color.BLACK);
		img2.setPadding(0, 0, 0, 0);
		gestureDetector = new GestureDetector(this);
	}

	
	public void setImages(){
		MyImageView image = (MyImageView) flipper.getCurrentView();
		imageDownloader.download(photos.get(indexPhotos).getPhotoURL(), image);
		Log.d("SETIMAGE", "SETIMAGE");
		image.setTag(Integer.toString(indexPhotos));
		if (myListener != null){
			myListener.onStart();
		}
	}
	
	
	public void next(){
		if (indexPhotos != photos.size()-1 ){
			MyImageView view = (MyImageView)getView(flipper.getDisplayedChild()+1);
			indexPhotos++;
			view.setTag(Integer.toString(indexPhotos));
			Log.d("IMAGE_TAG", view.getTag().toString());
			imageDownloader.download(photos.get(indexPhotos).getPhotoURL(), view);
			flipper.setInAnimation(inFromRight);
			flipper.setOutAnimation(outToLeft);
			if (myListener != null){
				myListener.onNext();
				
			}
			
			flipper.showNext();
		}
	}
	
	public void prev(){
		if (indexPhotos != 0){
			MyImageView view = (MyImageView)getView(flipper.getDisplayedChild()-1);
			indexPhotos--;
			view.setTag(Integer.toString(indexPhotos));
			imageDownloader.download(photos.get(indexPhotos).getPhotoURL(),view);
			
			flipper.setInAnimation(inFromLeft);
			flipper.setOutAnimation(outToRight);
			if (myListener != null){
				myListener.onPrev();
			}
			flipper.showPrevious();
		}
	}
	
	
	public View getView(int whichChild){
		View view = null;
		int mWhichChild = whichChild;
        if (whichChild >= flipper.getChildCount()) {
        	mWhichChild = 0;
        } else if (whichChild < 0) {
            mWhichChild = flipper.getChildCount() - 1;
        }	
        
        final int count = flipper.getChildCount();
        for (int i = 0; i < count; i++) {
        	if (i == mWhichChild){
            view = flipper.getChildAt(i);
            Log.d("VIEW", ""+i);
            break;
            }
        }
        Log.d("TAG", view.getTag().toString());
		return view;	
	}
	
	
	float x;
	float y;

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		gestureDetector.onTouchEvent(event);
		//Log.d("TOUCH", "MOVE");

		MyImageView view = (MyImageView) flipper.getCurrentView();
		// Handle touch events here...

		switch (event.getAction()) {
		case MotionEvent.ACTION_MOVE:
			// ...
			if (x < event.getX()) {
				view.changePos(event.getX() - x, 0);
			} else {
				view.changePos(event.getX() - x, 0);
			}
			break;
		case MotionEvent.ACTION_UP:
			view.setPos(0, 0);
			break;
		}

		x = event.getX();
		view.invalidate();
		return true;
	}

	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub

		return false;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		Log.d("Gesture", "Gesture2");
		float dx = Math.abs(velocityX);
		float dy = Math.abs(velocityY);
		if (dx > dy && dx > 150) {
			if (e1.getX() < e2.getX()) {
				prev();
			} else {
				next();
			}
			return true;
		}

		return false;
	}

	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return true;
	}

	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		Log.d("Gesture", "Gesture6");
		return false;
	}

	public boolean onDoubleTap(MotionEvent e) {
		// TODO Auto-generated method stub
		Log.d("Gesture", "Gesture7");
		return false;
	}

	public void setOnMyGalleryListener(MyGalleyListener myLis){
		myListener = myLis;
	}
	
	public boolean onDoubleTapEvent(MotionEvent e) {
		// TODO Auto-generated method stub
		Log.d("Gesture", "Gesture8");
		return false;
	}

	public boolean onSingleTapConfirmed(MotionEvent e) {
		// TODO Auto-generated method stub
		Log.d("Gesture", "Gesture9");
		return false;
	}

	public ViewFlipper getflipper() {
		return flipper;
	}

}
